<?php 
require_once('../../Connections/cotiza.php');
mysql_select_db($database_cotiza, $cotiza);
?>
<?php
function noCache() {
  header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
}

function num_decimales($num)
{
	$aux = explode(".",$num);
	return strlen($aux[1]);
}

function asigna_secuencia($tabla,$campo,$conexion,$len)
{
	$sufijo = "S";
	$query  = "SELECT max($campo) as id FROM $tabla";
	$maximo = mysql_query($query, $conexion) or die(mysql_error());
	$row    = mysql_fetch_assoc($maximo);
	$max    = $row['id'];

	if($max == "")
		$max = $sufijo."099999";
		
	$codigo = $max;
	$numero = "";
	$texto  = "";

	for($i=0;$i<strlen($codigo);$i++)
		if( is_numeric($codigo[$i]) )
			$numero .= $codigo[$i];
		else
			$texto  .= $codigo[$i];
	$numero = $numero + 1;
	
	$codigo = $texto;
	$ante   = $len-strlen($numero);
	for($i=0;$i<$ante;$i++)
		$codigo .= "0";
		
	//$numero = $numero +1;
	$codigo .= $numero;
	
	return $codigo;
}

function asigna_addr($conexion,$len)
{
	$sufijo = "C";
 	$query  = "SELECT Max(ad_mstr.ad_addr) AS id 
	             FROM ad_mstr
				WHERE substring(ad_mstr.ad_addr,1,1) =  'c' 
				  AND substring(ad_mstr.ad_addr,2,1) <>  'e'";
	$maximo = mysql_query($query, $conexion) or die(mysql_error());
	$row    = mysql_fetch_assoc($maximo);
	$max    = $row['id'];

	if($max == "")
		$max = $sufijo."0000001";
		
	$codigo = $max;
	$numero = "";
	$texto  = "";

	for($i=0;$i<strlen($codigo);$i++)
		if( is_numeric($codigo[$i]) )
			$numero .= $codigo[$i];
		else
			$texto  .= $codigo[$i];
	$numero = $numero + 1;
	
	$codigo = $texto;
	$ante   = $len-strlen($numero);
	for($i=0;$i<$ante;$i++)
		$codigo .= "0";
		
	//$numero = $numero +1;
	$codigo .= $numero;
	
	return $codigo;
}

function retorna_addr($conexion,$rut)
{
	$query     = "SELECT cm_addr FROM cm_mstr WHERE cm_rut LIKE '".$rut."%'";
	$rslt_addr = mysql_query($query,$conexion);
	$row_addr  = mysql_fetch_assoc($rslt_addr);
	
	return $row_addr['cm_addr'];
}

function traeCampo($tipo,$largo,$opciones,$name,$valor)
{
	if($tipo == "number")
		$campo    = "<input name='$name' type='text' class='default' id='$name' size='$largo' onkeypress='return valida_numeros(event)' autocomplete=\"off\" value='$valor' />";
	
	if($tipo == "text")
		$campo 	  = "<input name='$name' type='text' class='default' id='$name' size='$largo' autocomplete=\"off\" value='$valor'/>";
	
	if($tipo == "combobox")
	{
		$opciones = split(';',$opciones);
		$campo    = "<select class='default' name='$name' id='$name'>\n";
    	
		foreach($opciones as $op)
		{
			$selected = "";
			if($op == $valor)
				$selected = "selected='selected'";
				
			$campo .= "<option value='$op' ".$selected.">$op</option>";
		}
		$campo .= "</select>";
	}
	return $campo;	
}

function envia_correo($solicitud,$vendedor,$cliente,$producto,$caso)
{
	require("phpmailer/class.phpmailer.php");
	$mail	= new PHPMailer();
	$body	= "<img src=\"../img/logo.png\" width=\"314\" height=\"79\" /><br><br><table border=\"0\" style=\"font-family: Arial, Helvetica, sans-serif; font-size: 11px;\">
				 <tr>
					<td>Solicitud</td>
					<td>:</td>
					<td><strong>$solicitud</strong></td>
				 </tr>
				 <tr>
					<td>Vendedor</td>
					<td>:</td>
					<td><strong>$vendedor</strong></td>
				 </tr>
				 <tr>
					<td>Cliente</td>
					<td>:</td>
					<td><strong>$cliente</strong></td>
				 </tr>
				 <tr>
					<td>Producto</td>
					<td>:</td>
					<td><strong>$producto</strong></td>
				 </tr>
				 <tr>
					<td>Caso</td>
					<td>:</td>
					<td><strong>$caso</strong></td>
				 </tr>
				 </table>";
	$mail->IsSMTP();
	$mail->SMTPAuth   = true;                  // enable SMTP authentication
	//$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
	$mail->Host       = "smtp.entelchile.net";      // sets the SMTP server
	//$mail->Port       = 25;                   // set the SMTP port

	$mail->Username   = "cotizado@eroflex.cl";  // username
	$mail->Password   = "co1234";            // password
	
	$mail->AddReplyTo("cotizadoreroflex@eroflex.cl","Cotizador Eroflex");
	$mail->From       = "cotizadoreroflex@eroflex.cl";
	$mail->FromName   = "Cotizador Eroflex";

	$mail->Subject    = "Nueva solicitud generada";
	
	$mail->AltBody    = "Se ha generado una nueva solicitud desde el Cotizador"; // optional comment
	$mail->WordWrap   = 50; // set word wrap

	$mail->MsgHTML($body);

	$mail->AddAddress("mblanch@bcncons.cl", "Marco Blanch");

	//$mail->AddAttachment("images/phpmailer.gif");             // attachment

	$mail->IsHTML(true); // send as HTML
	$mail->Send();
	/*if(!$mail->Send()) 
		echo "Mailer Error: " . $mail->ErrorInfo;
	else 
	  echo "Message sent!";	*/
}

function asigna_valor($valor)
{
	if($valor != "")
		return $valor;
	return 0;
}

function retorna_valor_proc($solic,$proc,$campo)
{	
	$q   = "SELECT * FROM procd_det 
	         WHERE procd_solic_id = '$solic' 
			   AND procd_proc_id  = $proc 
			   AND procd_campo_id = $campo
			   AND procd_paso     = 'cotiza'";

	$r   = mysql_query($q,$GLOBALS['cotiza']);
	$row = mysql_fetch_assoc($r);
	
	return $row['procd_valor'];
}

function retorna_densidad_laminado($solic)
{
	$query = "SELECT * FROM solic_view WHERE solic_id = '$solic'";
	$rslt  = mysql_query($query,$GLOBALS['cotiza']);
	$row   = mysql_fetch_assoc($rslt);
	$material = $row['solic_material'];
	
	
	$tipo     = $row['mat_tipo'];
	if($tipo == "E")
	{
		$query    = "SELECT * FROM receta_view WHERE receta_solic_id = '$solic'";
		$rslt_rec = mysql_query($query,$GLOBALS['cotiza']);
		$row_rec  = mysql_fetch_assoc($rslt_rec);

		do{
			$densidad += $row_rec['pt_densidad'] * $row_rec['receta_pct_pt'] / 100;
		}while($row_rec  = mysql_fetch_assoc($rslt_rec));
		
		return $densidad;
	}
	
	if($tipo == "C")
	{
		$query    = "SELECT * FROM comprados_view WHERE compra_id_solicitud = '$solic'";
		$rslt_rec = mysql_query($query,$GLOBALS['cotiza']);
		$row_rec  = mysql_fetch_assoc($rslt_rec);
		
		return $row_rec['pt_densidad'];
	}
	
	if($tipo == "L")
	{
		$query    = "SELECT * FROM laminados_view WHERE matl_mat_id = '$material'";
		$rslt_lam = mysql_query($query,$GLOBALS['cotiza']);
		$row_lam  = mysql_fetch_assoc($rslt_lam);
		
		do{
			
		}while($row_lam  = mysql_fetch_assoc($rslt_lam));
	}
}

function retorna_espesor_laminado($solic)
{
	$query = "SELECT * FROM solic_view WHERE solic_id = '$solic'";
	$rslt  = mysql_query($query,$GLOBALS['cotiza']);
	$row   = mysql_fetch_assoc($rslt);
	
	$tipo     = $row['mat_tipo'];

	if($tipo == "E")
		return $row['solic_espesor1'];
		
	if($tipo == "C")
		return $row['solic_espesor1'];
		
	if($tipo == "L")
		return ($row['solic_espesor1'] + $row['solic_espesor2'] + $row['solic_espesor3']);
}

function calcula_densidad($mat,$solic,$id)
{
	$query    = "SELECT * FROM mat_mstr WHERE mat_id = '$mat'";
	$rslt_mat = mysql_query($query,$GLOBALS['cotiza']);
	$row_mat  = mysql_fetch_assoc($rslt_mat);
	$tipo     = $row_mat['mat_tipo'];

	if($tipo == "E")
	{
		$query    = "SELECT * FROM receta_view WHERE receta_solic_id = '$solic'";
		$rslt_ext = mysql_query($query,$GLOBALS['cotiza']);
		$row_ext  = mysql_fetch_assoc($rslt_ext);
		do{
			$densidad += $row_ext['pt_densidad'] * $row_ext['receta_pct_pt'] / 100;
		}while($row_ext  = mysql_fetch_assoc($rslt_ext));
		
		return $densidad;
	}
	
	if($tipo == "C")
	{
		$query    = "SELECT * FROM comprados_view WHERE compra_id_solicitud = '$solic' AND compra_id = $id";
		$rslt_com = mysql_query($query,$GLOBALS['cotiza']);
		$row_com  = mysql_fetch_assoc($rslt_com);
		return $row_com['pt_densidad'];
	}
	
	return 0;
}

function mes($mes)
{
	switch ($mes) {
    case "01"	:	return "Enero";
	case "02"	:	return "Febrero";
	case "03"	:	return "Marzo";
	case "04"	:	return "Abril";
	case "05"	:	return "Mayo";
	case "06"	:	return "Junio";
	case "07"	:	return "Julio";
	case "08"	:	return "Agosto";
	case "09"	:	return "Septiembre";
	case "10"	:	return "Octubre";
	case "11"	:	return "Noviembre";
	case "12"	:	return "Diciembre";
	}
}
?>